package com.yuyu.excel.config;

import com.alibaba.ttl.threadpool.TtlExecutors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class AsyncConfig {

    @Bean("taskExecutor")
    public Executor taskExecutor() {
        int corePoolSize = 10; // 核心线程数
        int maxPoolSize = 20; // 最大线程数
        long keepAliveTime = 60L; // 非核心线程空闲存活时间
        int queueCapacity = 100; // 队列容量
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(queueCapacity),
                new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
        );
        executor.allowCoreThreadTimeOut(true);
        return TtlExecutors.getTtlExecutorService(executor);
    }
}
